package com.xinwei.leetcode.数组;

import java.util.HashMap;

// https://leetcode-cn.com/problems/two-sum/
public class _1_两数之和 {
    public int[] twoSum(int[] nums, int target) {
        int a;
        for (int i = 0; i < nums.length; i++){
            a = target - nums[i];
            for (int j = i + 1; j < nums.length; j++){
                if (nums[j] == a){
                    System.out.println("{"+i+","+j+"}");
                }
            }
        }
        return null;
    }

    public int[] twoSum1(int[] nums, int target) {
        HashMap<Integer, Integer> hashMap = new HashMap<>(nums.length - 1);
        hashMap.put(nums[0], 0);
        for (int i = 1; i < nums.length; i++){
            int a = target - nums[i];
            if (hashMap.containsKey(a)) {
                return new int[]{i, hashMap.get(a)};
            }
            hashMap.put(nums[i], i);
        }
        return null;
    }
}
